package domain;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

import abolished.Start;

/**
 * Responsible for the surface elevation data
 * Elevation is with respect to Dutch mean sea level datum
 * Uses the mv.asc file from the the svp archive file.
 * @author kooijmanj1
 *
 */
public class Elevation {
		
	private int ncols; //in x-direction ncols elements
	private int nrows; //in y-direction nrows elements
	private double xllcorner; // 
	private double yllcorner;
	private double cellsize;
	private double nodata_value;
	private double[][] surfaceOnIndex;
	
				
	/**
	 * Read data from mv.asc file and populates metadata and elevation matrix on index
	 * @param reader
	 * @throws IOException
	 */
	public void populateElevationOnIndex(BufferedReader reader) throws IOException{
		String[] lineElements = null;
		lineElements = reader.readLine().split(" ");
		ncols = Integer.parseInt(lineElements[1]);
		lineElements = reader.readLine().split(" ");
		nrows = Integer.parseInt(lineElements[1]);
		lineElements = reader.readLine().split(" ");
		xllcorner = Double.parseDouble(lineElements[1]);
		lineElements = reader.readLine().split(" ");
		yllcorner = Double.parseDouble(lineElements[1]);
		lineElements = reader.readLine().split(" ");
		cellsize = Double.parseDouble(lineElements[1]);
		lineElements = reader.readLine().split(" ");
		nodata_value = Double.parseDouble(lineElements[1]);
		
		// read elevation data into matrix surfaceOnIndex
		String line = null;		
		surfaceOnIndex = new double[ncols][nrows];
		for (int y = 0 ; y < nrows; y++){
			line = reader.readLine();
			lineElements = line.split(" ");
			for(int x = 0; x < ncols; x++){	
				surfaceOnIndex[x][y] = Double.parseDouble(lineElements[x]);
			}
		}
	}
	
	public void populateCSVFromMatrix(PrintWriter writer){
		// write meta data to text file
		String ls = Start.newline;// line separator
		StringBuilder meta = new StringBuilder();
		meta.append("ncols " + ncols + ls);
		meta.append("nrows " + nrows + ls);
		meta.append("xllcorner " + xllcorner + ls);
		meta.append("yllcorner " + yllcorner + ls);
		meta.append("cellsize " + cellsize + ls);
		meta.append("NODATA_value " + nodata_value);
		writer.println(meta);		
		// write data to textfile
		for(int y = 0; y < nrows; y++){
			StringBuilder writeLine = new StringBuilder();
			for(int x = 0; x < ncols; x++){
				writeLine.append(surfaceOnIndex[x][y] + " ");
			}
			writer.println(writeLine);
		}
	}
	
	public int getNrOfColumns(){
		return ncols;
	}
	
	public int getNrOfRows(){
		return nrows;
	}
	
	public double getXllCornerOrdinate(){
		return xllcorner;
	}
	
	public double getYllCornerOrdinate(){
		return yllcorner;
	}
	
	public double getCellSize(){
		return cellsize;
	}
	
	public double getNoDataValue(){
		return nodata_value;
	}
	
	public double[][] getSurfaceOnIndex(){
		return surfaceOnIndex;
	}	
}
